const db = require("../../dbutil/index");
const sql = require("../../dbutil/sql");
const token = require("../../util/token");


module.exports = {
    // 获取首页被轮播的五条随机新闻
    getSwiperNews: (req, res) => {
        db.query(sql.news.getSwiperNews, [], (err, results) => {
            console.log(err);
            if (err) {
                res.send({ code: 505, msg: "数据库执行错误" });
            } else {
                res.send({ code: 200, msg: "成功获取到首页五条随机轮播新闻", data: results })
            }
        })
    },




    // 获取新闻首页内容
    getHomeNews: (req, res) => {
        db.query(sql.news.getHomeNews, [], (err, results) => {
            if (err) {
                res.send({ code: 505, msg: "数据库执行错误" });
            } else {
                res.send({ code: 200, msg: "成功获取新闻", data: results })
            }
        })
    },
    


    //获取新闻各类新闻首页内容
    getTypeNewHome: (req, res) => {
        let { category_id } = req.query;
        db.query(sql.news.getTypeNewHome, [category_id], (err, results) => {
            if (err) {
                res.send({ code: 505, msg: "数据库执行错误" });
            } else {
                res.send({ code: 200, msg: "成功获取新闻", data: results })
            }
        })
    },


    // 获取新闻详情
    getNewsdetail: (req, res) => {
        let { news_id } = req.query;
        db.query(sql.news.getNewsdetail, [news_id], (err, results) => {
            if (err) {
                res.send({ code: 505, msg: "数据库执行错误" });
            } else {
                // const data=JSON.parse(results[0].full_content);
                // console.log(results[0]);
                const data=results
                res.send({ code: 200, msg: "成功获取该新闻详情", data:data })
            }
        })
    },
    // getNewsdetailUser:(req, res) => {
    //     let { news_id,uid } = req.query;
    //     db.query(sql.news.getNewsdetail, [news_id], (err, results) => {
    //         if (err) {
    //             res.send({ code: 505, msg: "数据库执行错误" });
    //         } else {
    //             // const data=JSON.parse(results[0].full_content);
    //             // console.log(results[0]);

    //             // const data=results
    //             // res.send({ code: 200, msg: "成功获取该新闻详情", data:data })
    //             db.query(sql.users.addNewsHistory,[uid,news_id,Date.now()],(err2,results2)=>{
    //                 if (err2) {
    //                     res.send({ code: 505, msg: "数据库执行错误2" });
    //                 } else{
    //                     const data=results
    //                     res.send({ code: 200, msg: "ok", data:data })
    //                 }
    //             })
    //         }
    //     })
    // },


    // 搜索功能
    searchNews: (req, res) => {
        let { searchValue } = req.body;
        let params = [`%${searchValue}%`, `%${searchValue}%`]
        db.query(sql.news.searchNews, params, (err, results) => {
            if (err) {
                res.send({ code: 505, msg: "数据库执行错误" });
            } else {
                res.send({ code: 200, msg: "成功获取该条新闻", data: results})
            }
        })
    },


    //每条新闻的新闻被点赞总量
    getThumbsupTotal: (req, res) => {
        let { news_id } = req.query;
        db.query(sql.news.newsThumbsupTotal, [news_id], (err, results) => {
            if (err) {
                res.send({ code: 505, msg: "数据库操作失败" });
            } else {
                if (results.length > 0) {
                    res.send({ code: 200, msg: "成功获取每条新闻点赞总量", data: results[0] })
                } else {
                    res.send({ code: 404, msg: "没有找到该条新闻" })
                }
            }
        })
    },


    // 每条新闻的新闻被收藏总量
    getCollectTotal: (req, res) => {
        let { news_id } = req.query;
        db.query(sql.news.newsCollectdTotal, [news_id], (err, results) => {
            if (err) {
                res.send({ code: 505, msg: "数据库操作失败" });
            } else {
                if (results.length > 0) {
                    res.send({ code: 200, msg: "成功获取每条新闻收藏总量", data: results[0] })
                } else {
                    res.send({ code: 404, msg: "没有找到该条新闻" })
                }
            }
        })
    },
    renderAddNews:(req,res)=>{
        db.query(sql.news.renderAddNews,(err,results)=>{
            if(err){
                res.send({ code: 505, msg: "数据库操作失败" });
            }else {
                if (results.length > 0) {
                    res.send({ code: 200, msg: "渲染发布新闻", data: results })
                } else {
                    res.send({ code: 404, msg: "没有找到该条新闻" })
                }
            }
        })
    }


}


